import { Component, Input } from '@angular/core';

import { ComponentBase } from '../share';

export class Person {
    assemblyContext: (index: number, person: Person) => any;

    constructor(
        public id: number,
        public name: string,
        public nameTemplate: string,
        public nameContext?: any) {

    }
}

@Component({
    selector: 'primeng-table',
    templateUrl: './primeng.table.component.html'
})
export class PrimeNgTableComponent extends ComponentBase {
    private _persons: Person[];

    @Input() get persons(): Person[] {
        return this._persons;
    }

    set persons(value: Person[]) {
        try {

            this._persons = this.preprocessingPersons(value);
            this.internalConsoleLog(`persons ${JSON.stringify(this._persons)}`);
        } catch (error) {
            this.internalConsoleLog(`persons setting error ${error}`);
        }
    }

    private preprocessingPersons(persons: Person[]): Person[] {
        const ps = persons || [];

        ps.forEach(p => {
            p.assemblyContext = (index: number, person: Person): any => this.assemblyPersonNameContext(index, person);
        });

        return ps;
    }

    private assemblyPersonNameContext(index: number, person: Person): any {
        person.nameContext =  person.nameContext || {};
        person.nameContext.index = index;
        person.nameContext.name = person.name;
        person.nameContext.id = index;

        this.internalConsoleLog(`${index} assemblyPersonNameContext`);

        return person.nameContext;
    }

}
